package com.sunday.ble.orlant.utils;

import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sunday.ble.orlant.R;
import com.sunday.ble.orlant.adapter.BlueToothAdapter;
import com.sunday.ble.orlant.greendao.Device;
import com.sunday.common.widgets.taosty.Toasty;

import java.util.ArrayList;
import java.util.List;


/**
 * 仿IOS的弹出框
 *
 * @来自网上
 */
public class BluetoothDialogView extends Dialog {

    private Context context;
    private String title;
    private List<BluetoothDevice> bluetoothDeviceList = new ArrayList<>();
    private BlueToothAdapter blueToothAdapter;
    private ClickListenerInterface clickListenerInterface;
    private List<Device> adapterShowList;//用来显示

    public BluetoothDialogView(Context context, String title,List<BluetoothDevice> bluetoothDeviceList, List<Device> adapterShowList) {
        super(context, R.style.UIAlertViewStyle);

        this.context = context;
        this.title = title;
        this.bluetoothDeviceList = bluetoothDeviceList;
        this.adapterShowList = adapterShowList;
    }

    public interface ClickListenerInterface {
        void doConnect(BluetoothDevice device,Device device1);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }

    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.bluetooth_dialog_view, null);
        setContentView(view);
        ListView listView = (ListView) view.findViewById(R.id.list_view);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

        if ("".equals(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        blueToothAdapter = new BlueToothAdapter(context,bluetoothDeviceList,adapterShowList);
        listView.setAdapter(blueToothAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (clickListenerInterface!=null){
                    clickListenerInterface.doConnect(bluetoothDeviceList.get(position),adapterShowList.get(position));
                }
            }
        });
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    public void refreshAdapter(List<BluetoothDevice> bluetoothDeviceList,List<Device> adapterShowList){
//        this.bluetoothDeviceList.clear();
//        this.adapterShowList.clear();
        this.bluetoothDeviceList = bluetoothDeviceList;
        this.adapterShowList = adapterShowList;
        blueToothAdapter.notifyDataSetChanged();
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

}
